package com.truthbean.jutils.exception;

import java.io.PrintWriter;
import java.io.StringWriter;

public class ExceptionUtils {

    private ExceptionUtils() {
        //no instance
    }

    /**
     * 如果目标为空则抛出异常
     *
     * @param target
     * @param errorMessage
     */
    public static void throwIfNull(Object target, String errorMessage) {
        if (target == null) {
            throw new BusinessException(errorMessage);
        }
    }

    /**
     * 如果目标为空则抛出异常 本方法空指针安全
     *
     * @param target
     * @param errorMessage
     */
    public static void throwIfEmpty(String target, String errorMessage) {
        if (target == null || target.equals("")) {
            throw new BusinessException(errorMessage);
        }
    }

    public static String getExceptionMessage(Exception ex) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        ex.printStackTrace(pw);
        return sw.toString();
    }

}
